import os
import random

# 单词组抽查
# 单词表抽查
# 单词库抽查

def wordSck(base):
    os.system("clear")

    def showMethod():
        print("a.单词组抽查")
        print("b.单词表抽查")
        print("c.返回上一级")

    def stkWord(stkWords):
        os.system("clear")
        while True:
            print("1.根据中文来抽查英文")
            print("2.根据英文来抽查中文")
            iptIfLang = input("请输入抽查方式的序号：")
            if iptIfLang == 'exit':
                os.system("clear")
                showMethod()
                break
            os.system("clear")
            hasStkWord = []
            wordsLen = len(stkWords)
            if iptIfLang == '1':
                print("提示：输入'exit()'即可退出")
                while True:
                    randNum = random.randint(0, wordsLen - 1)
                    if len(hasStkWord) == wordsLen:
                        input('提示：本轮单词已经全部抽查完(按任意键继续)')
                        os.system("clear")
                        break

                    if randNum in hasStkWord:
                        continue

                    stkChinese, stkEnglish = stkWords[randNum]
                    ret = input("%s: " % stkChinese)
                    if ret == stkEnglish:
                        print("回答正确，正确答案：%s" % stkEnglish)

                    elif ret == 'exit()':
                        os.system("clear")
                        break

                    else:
                        print("回答错误，正确答案：%s" % stkEnglish)
                    hasStkWord.append(randNum)
                    
            elif iptIfLang == '2':
                print("提示：输入'exit()'即可退出")
                while True:
                    randNum = random.randint(0, wordsLen - 1)
                    if len(hasStkWord) == wordsLen:
                        input('提示：本轮单词已经全部抽查完(按任意键继续)')
                        os.system("clear")
                        break
                    
                    if randNum in hasStkWord:
                        continue

                    stkChinese, stkEnglish = stkWords[randNum]
                    ret = input("%s: " % stkEnglish)
                    if ret == stkEnglish:
                        print("回答正确，正确答案：%s" % stkChinese)
                    
                    elif ret == 'exit()':
                        os.system("clear")
                        break

                    else:
                        print("回答错误，正确答案：%s" % stkChinese)
                    hasStkWord.append(randNum)
        
            else:
                print("错误：输入错误，请重试")
                continue
        os.system("clear")
        showMethod()


    def showAllTables():
        allTables = base.selectAllTable()
        allTables.remove(('sqlite_sequence',))
        if not len(allTables):
            print("警告：没有单词表，请先添加")
            return False
        print("提示：以下为所有的单词表")
        for i in allTables:
            print("{}.{}".format(str(allTables.index(i) + 1), i[0]))
        return allTables

    showMethod()
    while True:
        ipt = input("请输入序号：")
        if ipt == 'exit' or ipt == 'c':
            break

        elif ipt == 'a':
            allTables = showAllTables()
            if (not allTables):
                continue
            iptTable = input("请输入单词所在的单词表：")
            if iptTable == 'exit':
                continue
            iptGroup = input("请输入单词所在的组(自己查!!!)：")
            if iptGroup == 'exit':
                continue
            try:
                base.cur.execute('select chinese, english from {} where "group"="{}"'.format(allTables[int(iptTable) - 1][0], iptGroup))
                stkWords = base.cur.fetchall()
            except Exception as e:
                # print(e)
                print("错误：此操作出错，请重试")
            else:
                os.system("clear")
                print("提示：单词获取成功")
                print("1.是")
                print("2.否")
                iptIfStk = input("是否现在开始抽查：")
                if iptIfStk == 'exit' or iptIfStk == '2':
                    os.system("clear")
                    showMethod()
                    continue
                stkWord(stkWords)
                
        elif ipt == 'b':
            allTables = showAllTables()
            if (not allTables):
                continue
            iptTable = input("请输入单词所在的单词表：")
            if iptTable == 'exit':
                continue

            try:
                base.cur.execute('select chinese, english from {}'.format(allTables[int(iptTable) - 1][0]))
                stkWords = base.cur.fetchall()
            except Exception as e:
                # print(e)
                print("错误：此操作出错，请重试")
            else:
                os.system("clear")
                print("提示：单词获取成功")
                print("1.是")
                print("2.否")
                iptIfStk = input("是否现在开始抽查：")
                if iptIfStk == 'exit' or iptIfStk == '2':
                    os.system("clear")
                    showMethod()
                    continue
                stkWord(stkWords)

        else:
            print("错误：没有此方法")
